<?php

class BudgetController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules(){
	    return array(
	        array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
	        array('deny', // deny anything else
	              'users'=>array('*'),
	        ),
	    );
	}

    public function actionIndex()
    {   
            $this -> render('index',array(	

            ));
    }    		
	
	public function actionList()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $model = new Budget();

            $data = $model -> getList($_POST['hyipId']);

            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
    }
	
    public function actionCreate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $model = new Budget();
            $model -> setData($_POST['models'][0]);
			
            $format = "Y-m-d";

            if($model -> startDate != "")
                    $model -> startDate = date($format, strtotime(str_replace("/", "-", $model -> startDate)));

            if($model -> endDate != "")
                    $model -> endDate = date($format, strtotime(str_replace("/", "-", $model -> endDate)));			

            if($model -> save())
            {
                $hyip = new Hyip();
                $hyip -> updateBudget($model['hyipId']);
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionUpdate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $data = $_POST['models'][0];
            
            $model = Budget::model() -> findByPk($data['id']);
            
            unset($data['id']);
            $model -> setData($data);
            
            unset($data['id']);
            
            $format = "Y-m-d";

            if($model -> startDate != "")
                    $model -> startDate = date($format, strtotime(str_replace("/", "-", $model -> startDate)));

            if($model -> endDate != "")
                    $model -> endDate = date($format, strtotime(str_replace("/", "-", $model -> endDate)));
            
            if($model -> save())
            {
                $hyip = new Hyip();
                $hyip -> updateBudget($model['hyipId']);
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }      
    
    public function actionDelete()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $model = $_POST['models'][0];			
            $item = Budget::model() -> findByPk($model['id']);
            if ($item -> delete()) {
                $hyip = new Hyip();
                $hyip -> updateBudget($model['hyipId']);
                 echo json_encode(array("status" => 'success'));
             }
             else{
                echo json_encode(array("status" => 'failure'));
             }					
        }
        
        return;
    }      		
}